﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;
using DAT = CPPEI.ETL.Domain.Data;

namespace CPPEI.ETL.WebApi.Summary
{
    /// <summary>
    /// 获取任务的概要信息
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("Summary", "GetTaskList", "1.0")]
    public class GetTaskListAction : BaseAction<GetTaskListParams, GetTaskListResult>
    {
        protected override ActionResult<GetTaskListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetTaskListParams opParams)
        {
            SER.QueryService service = new SER.QueryService();
            SingleResult<DAT.TaskSummary> result = service.Query4Task();
            if(result.IsSuccess)
            {
                GetTaskListResult actResult = new GetTaskListResult();
                actResult.Total  = result.Data.Total;
                actResult.Timer = result.Data.Timer;
                actResult.Alarm = result.Data.Alarm;
                actResult.Manual = result.Data.Manual;
                actResult.Event = result.Data.Event;

                for (int index = 0; index < result.Data.Tasks.Count; index++)
                {
                    GetTaskListResult.TaskExecuteRecord taskCount = new GetTaskListResult.TaskExecuteRecord();
                    taskCount.Id = result.Data.Tasks[index].Id;
                    taskCount.Name = result.Data.Tasks[index].Name;
                    if (result.Data.Tasks[index].BatchId != null)
                    {
                        taskCount.BatchId = result.Data.Tasks[index].BatchId.Value;
                        taskCount.ExecutedTime = result.Data.Tasks[index].ExecutedTime.Value.ToString("yyyy-MM-dd");
                        taskCount.DataCount = result.Data.Tasks[index].DataCount.Value;
                        taskCount.InsertSuccessCount = result.Data.Tasks[index].InsertSuccessCount.Value;
                    }
                    else
                    {
                        taskCount.BatchId = Guid.Empty;// result.Data.Tasks[index].BatchId.Value;
                        taskCount.ExecutedTime = string.Empty;// result.Data.Tasks[index].ExecutedTime.Value;
                        taskCount.DataCount = 0;// result.Data.Tasks[index].DataCount.Value;
                        taskCount.InsertSuccessCount = 0;// result.Data.Tasks[index].InsertSuccessCount.Value;
                    }
                    taskCount.RunMode = result.Data.Tasks[index].RunMode;
                    actResult.Tasks.Add(taskCount);
                }

                return new ActionResult<GetTaskListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetTaskListResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class GetTaskListParams
    {
    }

    public class GetTaskListResult
    {
        public int Total { get; set; }

        public int Timer { get; set; }
        public int Alarm { get; set; }
        public int Manual { get; set; }
        public int Event { get; set; }

        public List<TaskExecuteRecord> Tasks = new List<TaskExecuteRecord>();

        public class TaskExecuteRecord
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public Guid BatchId { get; set; }
            public string ExecutedTime { get; set; }
            public int RunMode { get; set; }
            public long DataCount { get; set; }
            public long InsertSuccessCount { get; set; }
        }
    }
}
